package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import rmi.Answer;
import rmi.Question;
import rmi.Session;

public class GUIQuestionRandom extends JPanel{
	private static final long serialVersionUID = 1L;
	private static final String NON_CHOOSE ="Bạn muốn thử qua một vài câu hỏi?";
	private Session session;
	private int listID;
	private ArrayList<Integer> listInt;
	
	private Question question;
	private int trueAnswer;
	private ArrayList<Integer> answerID;
	
	private JComboBox<String> cbList;
	private JLabel lbCount;
	private JTextArea lbQuestion;
	private JLabel lbImage;
	private ArrayList<JRadioButton> arrayJradio;
	private JButton next, summit;
	private JLabel lbInfo;
	private JPanel questionPanel;
	private JPanel answerPanel;
	private ButtonGroup group;
	
	public GUIQuestionRandom(Session session) {
		this.session = session;
		setBorder(new EmptyBorder(10, 10, 10, 10));
		setLayout(new BorderLayout());
		setOpaque(false);
		
		listInt = new ArrayList<>();
		cbList = new JComboBox<>();
		cbList.addItem(NON_CHOOSE);
		listInt.add(-1);
		LinkedHashMap<Integer, String> list;
		try {
			list = this.session.getRandomList();
			for(Entry<Integer, String> count : list.entrySet()){
				listInt.add(count.getKey());
				cbList.addItem(count.getValue());
			}
		} catch (RemoteException | SQLException e) {
			e.printStackTrace();
		}
		
		cbList.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange() == ItemEvent.SELECTED){
					if(cbList.getSelectedIndex() == 0){
						setShowing(false);
						return;
					}
					int index = cbList.getSelectedIndex();
					listID = listInt.get(index);
					nextQuestion();
				}
			}
		});
		
		lbCount = new JLabel();
		lbCount.setFont(lbCount.getFont().deriveFont(20f));
		lbCount.setForeground(new Color(30,30,30));
		
		lbQuestion = new CustomJTextArea();
		
		lbImage = new JLabel();
		
		lbInfo = new JLabel(" ");
		lbInfo.setFont(lbInfo.getFont().deriveFont(16f));
		lbInfo.setForeground(Color.RED);
		
		arrayJradio = new ArrayList<>();
		answerID = new ArrayList<>();
		
		group = new  ButtonGroup();
		
		answerPanel = new JPanel();
		answerPanel.setLayout(new BoxLayout(answerPanel, BoxLayout.Y_AXIS));
		answerPanel.setOpaque(false);
		
		next = new CustomJButton(">", 40, 40, Color.WHITE, CustomJButton.DARKER,
				Color.DARK_GRAY, 0, true, true);
		next.addActionListener(new nextAction());
		summit = new CustomJButton("S", 40, 40, Color.WHITE, CustomJButton.DARKER,
				Color.DARK_GRAY, 0, true, true);
		summit.addActionListener(new summitAction());
		//addtoPanel
		questionPanel = new JPanel(new GridBagLayout());
		questionPanel.setOpaque(false);
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx =1;
		c.weighty =0;
		questionPanel.add(lbCount, c);
		
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 2;
		questionPanel.add(lbQuestion, c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		questionPanel.add(lbImage, c);
		
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 2;
		questionPanel.add(answerPanel,c);
		
		JPanel buttonPanel = new JPanel(new BorderLayout());
		buttonPanel.setOpaque(false);
		JPanel centerButtons = new JPanel(new FlowLayout(FlowLayout.CENTER));
		centerButtons.setOpaque(false);
		
		centerButtons.add(next);
		centerButtons.add(summit);
		buttonPanel.add(lbInfo,BorderLayout.NORTH);
		buttonPanel.add(centerButtons,BorderLayout.SOUTH);
		add(cbList,BorderLayout.NORTH);
		
		JPanel pn = new CustomVerticalScrollJpanel();
		pn.setOpaque(false);
		pn.setLayout(new BoxLayout(pn,BoxLayout.PAGE_AXIS));
		pn.add(questionPanel);
		JScrollPane scroll = new JScrollPane(pn, 
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setBorder( new EmptyBorder(0, 0, 0, 0));
		scroll.setOpaque(false);
		scroll.getViewport().setOpaque(false);
		add(scroll,BorderLayout.CENTER);
		add(buttonPanel,BorderLayout.SOUTH);
		setShowing(false);
	}
	
	public void nextQuestion(){
		try {
			LinkedHashMap<Integer, Question> temp = session.getRandomQuestion(listID);
			if(temp.size() == 0){
				setShowing(false);
				return;
			}else{
				setShowing(true);
			}
			lbInfo.setText("  ");
			lbImage.setIcon(null);
			question = temp.entrySet().iterator().next().getValue();
			trueAnswer = temp.entrySet().iterator().next().getKey();
			displayQuestion(question);
		} catch (RemoteException | SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void setShowing(boolean isShow){
		lbQuestion.setText("");
		this.lbCount.setText("");
		this.lbImage.setIcon(null);
		group = new ButtonGroup();
		arrayJradio.clear();
		answerID.clear();
		answerPanel.removeAll();
		next.setEnabled(isShow);
		summit.setEnabled(isShow);
	}
	
	public void displayQuestion(Question question){
		this.lbCount.setText("Câu Hỏi:");
		this.lbQuestion.setText(question.getQuestion());
		group = new ButtonGroup();
		arrayJradio.clear();
		answerID.clear();
		answerPanel.removeAll();
		if(question.getQuestionImageLink().trim().equals("")){
		}else{
			try {
				lbImage.setIcon(session.getImage(question.getQuestionID()));
				if(lbImage.getIcon() == null){
					lbImage.setText("Nạp hình ảnh không thành công!");
				}
			} catch (RemoteException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(getParent(), "Lỗi trong quá trình lấy ảnh từ máy chủ.", 
						"Thông báo", JOptionPane.ERROR_MESSAGE);
			}
		}
		
		for(Entry<Integer, Answer> zzz : question.getMapAnswers().entrySet()){
			if(!zzz.getValue().isShow()) continue;
			JRadioButton bt = new JRadioButton();
			JTextArea ta = new CustomJTextArea();
			ta.setText(zzz.getValue().getAnswerContent());
			ta.setWrapStyleWord(true);
			ta.setLineWrap(true);
			
			bt.setOpaque(false);
			bt.setFont(bt.getFont().deriveFont(16f));
			group.add(bt);
			arrayJradio.add(bt);
			
			JPanel answerCell = new JPanel(new GridBagLayout());
			answerCell.setOpaque(false);
			GridBagConstraints c = new GridBagConstraints();
			c.fill = GridBagConstraints.HORIZONTAL;
			c.gridx = 0;
			c.gridy = 0;
			answerCell.add(bt,c);
			c.gridx = 1;
			c.gridy = 0;
			c.weightx = 1;
			answerCell.add(ta,c);
			answerPanel.add(answerCell);
			answerID.add(zzz.getKey());
		}
	}
	
	class nextAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			nextQuestion();
			summit.setEnabled(true);
			lbInfo.setText("  ");
		}
	}
	
	class summitAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			for(int i =0;i<arrayJradio.size();i++){
				if(arrayJradio.get(i).isSelected()){
					if(trueAnswer == answerID.get(i)){
						lbInfo.setText("Đáp án bạn vừa chọn là ĐÚNG");
					}else{
						lbInfo.setText("Đáp án bạn vừa chọn là SAI");
					}
					summit.setEnabled(false);
					return;
				}
			}
			lbInfo.setText("Vui lòng chọn câu trả lời!");
		}
	}
}
